﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.Hosting;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class SQLReportController
    {
        public string AddStoreCSSforEmailHTML(string dataHTML)
        {
            string path = HostingEnvironment.MapPath(@"\desktopmodules\NB_Store\module.css");
            if (File.Exists(path))
            {
                FileObj obj2 = new FileObj();
                string errInfo = "";
                dataHTML = "<style type=\"text/css\">" + obj2.GetFileContents(path, ref errInfo) + "</style>" + dataHTML;
            }
            return dataHTML;
        }

        public string BuildReportInputForm(string reportID)
        {
            IEnumerator enumerator;
            FormTemplateBuilder builder = new FormTemplateBuilder();
            NB_Store_SQLReportInfo sQLReport = this.GetSQLReport(Conversions.ToInteger(reportID));
            builder.AddTitle(sQLReport.ReportName);
            ArrayList sQLReportParamList = this.GetSQLReportParamList(Conversions.ToInteger(reportID));
            string id = "";
            try
            {
                enumerator = sQLReportParamList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_SQLReportParamInfo current = (NB_Store_SQLReportParamInfo) enumerator.Current;
                    id = Strings.Replace(current.ParamName, "@", "", 1, -1, CompareMethod.Binary);
                    switch (current.ParamSource)
                    {
                        case 8:
                        {
                            NBright_TextBox ctrl = new NBright_TextBox {
                                Text = current.ParamValue
                            };
                            builder.AddControl(id, ctrl, Strings.Replace(current.ParamName, "@", "", 1, -1, CompareMethod.Binary) + " : ");
                            break;
                        }
                        case 9:
                        {
                            NBright_dateEditControl control = new NBright_dateEditControl {
                                Text = current.ParamValue
                            };
                            builder.AddControl(id, control, Strings.Replace(current.ParamName, "@", "", 1, -1, CompareMethod.Binary) + " : ");
                            break;
                        }
                        case 10:
                        {
                            NBright_DropDownList list2 = new NBright_DropDownList {
                                data = current.ParamValue,
                                datavalue = current.ParamValue
                            };
                            builder.AddControl(id, list2, Strings.Replace(current.ParamName, "@", "", 1, -1, CompareMethod.Binary) + " : ");
                            break;
                        }
                        case 11:
                        {
                            NBright_RadioButtonList list3 = new NBright_RadioButtonList {
                                data = current.ParamValue,
                                datavalue = current.ParamValue
                            };
                            builder.AddControl(id, list3, Strings.Replace(current.ParamName, "@", "", 1, -1, CompareMethod.Binary) + " : ");
                            break;
                        }
                        case 12:
                        {
                            NBright_CheckBox box2 = new NBright_CheckBox();
                            if (current.ParamValue != "")
                            {
                                try
                                {
                                    box2.Checked = Conversions.ToBoolean(current.ParamValue);
                                }
                                catch (Exception exception1)
                                {
                                    ProjectData.SetProjectError(exception1);
                                    Exception exception = exception1;
                                    ProjectData.ClearProjectError();
                                }
                            }
                            builder.AddControl(id, box2, Strings.Replace(current.ParamName, "@", "", 1, -1, CompareMethod.Binary) + " : ");
                            break;
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return builder.GetFormTemplate();
        }

        public void CopyReport(int ReportID)
        {
            IEnumerator enumerator;
            IEnumerator enumerator2;
            NB_Store_SQLReportInfo sQLReport = this.GetSQLReport(ReportID);
            sQLReport.ReportID = -1;
            sQLReport.ReportName = sQLReport.ReportName + " [Copy]";
            int num = this.UpdateObjSQLReport(sQLReport);
            ArrayList sQLReportParamList = this.GetSQLReportParamList(ReportID);
            try
            {
                enumerator = sQLReportParamList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_SQLReportParamInfo current = (NB_Store_SQLReportParamInfo) enumerator.Current;
                    current.ReportID = num;
                    current.ReportParamID = -1;
                    this.UpdateObjSQLReportParam(current);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            sQLReportParamList = this.GetSQLReportXSLList(ReportID);
            try
            {
                enumerator2 = sQLReportParamList.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    NB_Store_SQLReportXSLInfo objInfo = (NB_Store_SQLReportXSLInfo) enumerator2.Current;
                    objInfo.ReportID = num;
                    objInfo.ReportXSLID = -1;
                    this.UpdateObjSQLReportXSL(objInfo);
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
        }

        public void DeleteSQLReport(int ReportID)
        {
            DataProvider.Instance().DeleteSQLReport(ReportID);
        }

        public void DeleteSQLReportParam(int ReportParamID)
        {
            DataProvider.Instance().DeleteSQLReportParam(ReportParamID);
        }

        public void DeleteSQLReportXSL(int ReportXSLID)
        {
            DataProvider.Instance().DeleteSQLReportXSL(ReportXSLID);
        }

        public IDataReader ExecuteSQL(string SQLcommand)
        {
            return DataProvider.Instance().ExecuteSQL(SQLcommand);
        }

        public string ExecuteSQLReportText(string SQLcommand, string FieldDelimeter, string FieldQualifier, bool ExportHeader)
        {
            return DataProvider.Instance().ExecuteSQLReportText(SQLcommand, FieldDelimeter, FieldQualifier, ExportHeader);
        }

        public string ExecuteSQLReportXml(string SQLcommand)
        {
            return DataProvider.Instance().ExecuteSQLReportXml(SQLcommand);
        }

        public string GetReportOutput(NB_Store_SQLReportInfo objRinfo)
        {
            string str = "";
            if (objRinfo != null)
            {
                objRinfo.SQL = this.insertParams(objRinfo.ReportID, -1, objRinfo.PortalID, null, objRinfo.SQL, SharedFunctions.GetCurrentCulture());
                string str3 = "";
                DataGrid gridView = new DataGrid();
                str3 = this.runXSL(objRinfo);
                if (str3 == "")
                {
                    if (objRinfo.SQL != "")
                    {
                        this.popDataGridSQL(objRinfo.SQL, gridView);
                        StringBuilder sb = new StringBuilder();
                        StringWriter writer2 = new StringWriter(sb);
                        HtmlTextWriter writer = new HtmlTextWriter(writer2);
                        gridView.GridLines = GridLines.None;
                        gridView.Width = Unit.Percentage(100.0);
                        gridView.HeaderStyle.CssClass = "NBright_HeaderStyle";
                        gridView.FooterStyle.CssClass = "NBright_FooterStyle";
                        gridView.EditItemStyle.CssClass = "NBright_EditItemStyle";
                        gridView.SelectedItemStyle.CssClass = "NBright_SelectedItemStyle";
                        gridView.AlternatingItemStyle.CssClass = "NBright_AlternatingItemStyle";
                        gridView.ItemStyle.CssClass = "NBright_ItemStyle";
                        gridView.AllowPaging = false;
                        gridView.RenderControl(writer);
                        str = sb.ToString();
                    }
                    return str;
                }
                if (str3 != "HIDE_XSL_OUTPUT")
                {
                    str = str3;
                }
            }
            return str;
        }

        public string GetReportOutput(int PortalID, int RepID)
        {
            NB_Store_SQLReportInfo sQLReportByRef = this.GetSQLReportByRef(PortalID, Conversions.ToString(RepID));
            return this.GetReportOutput(sQLReportByRef);
        }

        public string GetReportOutput(int PortalID, string RepRef)
        {
            NB_Store_SQLReportInfo sQLReportByRef = this.GetSQLReportByRef(PortalID, RepRef);
            return this.GetReportOutput(sQLReportByRef);
        }

        public ArrayList GetSQLAdminReportList(int PortalID, bool IsEditable, string SearchText)
        {
            return CBO.FillCollection(DataProvider.Instance().GetSQLAdminReportList(PortalID, IsEditable, SearchText), typeof(NB_Store_SQLReportInfo));
        }

        public NB_Store_SQLReportInfo GetSQLReport(int ReportID)
        {
            return (NB_Store_SQLReportInfo) CBO.FillObject(DataProvider.Instance().GetSQLReport(ReportID), typeof(NB_Store_SQLReportInfo));
        }

        public NB_Store_SQLReportInfo GetSQLReportByRef(int PortalID, string ReportRef)
        {
            return (NB_Store_SQLReportInfo) CBO.FillObject(DataProvider.Instance().GetSQLReportByRef(PortalID, ReportRef), typeof(NB_Store_SQLReportInfo));
        }

        public NB_Store_SQLReportParamInfo GetSQLReportParam(int ReportParamID)
        {
            return (NB_Store_SQLReportParamInfo) CBO.FillObject(DataProvider.Instance().GetSQLReportParam(ReportParamID), typeof(NB_Store_SQLReportParamInfo));
        }

        public ArrayList GetSQLReportParamList(int ReportID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetSQLReportParamList(ReportID), typeof(NB_Store_SQLReportParamInfo));
        }

        public NB_Store_SQLReportXSLInfo GetSQLReportXSL(int ReportXSLID)
        {
            return (NB_Store_SQLReportXSLInfo) CBO.FillObject(DataProvider.Instance().GetSQLReportXSL(ReportXSLID), typeof(NB_Store_SQLReportXSLInfo));
        }

        public ArrayList GetSQLReportXSLList(int ReportID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetSQLReportXSLList(ReportID), typeof(NB_Store_SQLReportXSLInfo));
        }

        public string insertFormParams(int ReportID, string strSQL, string FormXMLData)
        {
            IEnumerator enumerator;
            SQLReportController controller = new SQLReportController();
            string str3 = "";
            ArrayList sQLReportParamList = controller.GetSQLReportParamList(ReportID);
            strSQL = "  " + strSQL;
            try
            {
                enumerator = sQLReportParamList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string str4;
                    NB_Store_SQLReportParamInfo current = (NB_Store_SQLReportParamInfo) enumerator.Current;
                    str3 = Strings.Replace(current.ParamName, "@", "", 1, -1, CompareMethod.Binary);
                    if (!current.ParamName.StartsWith("@"))
                    {
                        current.ParamName = "@" + current.ParamName;
                    }
                    string str2 = "";
                    if (current.ParamSource >= 8)
                    {
                        str2 = str2 + " declare " + current.ParamName + " " + current.ParamType + " ";
                        switch (current.ParamSource)
                        {
                            case 8:
                                str2 = str2 + " set " + current.ParamName + " = '" + SharedFunctions.getGenXMLvalue(FormXMLData, "genxml/textbox/" + str3) + "' ";
                                goto Label_0460;

                            case 9:
                                str4 = SharedFunctions.getGenXMLvalue(FormXMLData, "genxml/textbox/" + str3);
                                if (Information.IsDate(str4))
                                {
                                    if (current.ParamType.ToLower() != "datetime")
                                    {
                                        goto Label_0281;
                                    }
                                    str2 = str2 + " set " + current.ParamName + " = convert(datetime,'" + Conversions.ToDate(str4).Year.ToString() + "-" + Conversions.ToDate(str4).Month.ToString("00") + "-" + Conversions.ToDate(str4).Day.ToString("00") + "') ";
                                }
                                goto Label_0460;

                            case 10:
                                str2 = str2 + " set " + current.ParamName + " = '" + SharedFunctions.getGenXMLvalue(FormXMLData, "genxml/dropdownlist/" + str3) + "' ";
                                goto Label_0460;

                            case 11:
                                str2 = str2 + " set " + current.ParamName + " = '" + SharedFunctions.getGenXMLvalue(FormXMLData, "genxml/radiobuttonlist/" + str3) + "' ";
                                goto Label_0460;

                            case 12:
                                if (SharedFunctions.getGenXMLvalue(FormXMLData, "genxml/checkbox/" + str3).ToLower() != "true")
                                {
                                    goto Label_0444;
                                }
                                str2 = str2 + " set " + current.ParamName + " = '1' ";
                                goto Label_0460;
                        }
                    }
                    goto Label_0460;
                Label_0281:;
                    str2 = str2 + " set " + current.ParamName + " = '" + Conversions.ToDate(str4).Year.ToString() + "-" + Conversions.ToDate(str4).Month.ToString("00") + "-" + Conversions.ToDate(str4).Day.ToString("00") + "' ";
                    goto Label_0460;
                Label_0444:
                    str2 = str2 + " set " + current.ParamName + " = '0' ";
                Label_0460:
                    strSQL = str2 + " " + strSQL;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return strSQL;
        }

        public string insertParams(int ReportID, int UserId, int PortalId, HttpRequest Request, string strSQL, string Lang)
        {
            IEnumerator enumerator;
            ArrayList sQLReportParamList = new SQLReportController().GetSQLReportParamList(ReportID);
            strSQL = "  " + strSQL;
            try
            {
                enumerator = sQLReportParamList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_SQLReportParamInfo current = (NB_Store_SQLReportParamInfo) enumerator.Current;
                    if (!current.ParamName.StartsWith("@"))
                    {
                        current.ParamName = "@" + current.ParamName;
                    }
                    string str2 = "";
                    if (current.ParamSource < 8)
                    {
                        str2 = str2 + " declare " + current.ParamName + " " + current.ParamType + " ";
                        switch (current.ParamSource)
                        {
                            case 1:
                                str2 = str2 + " set " + current.ParamName + " = '" + current.ParamValue + "' ";
                                break;

                            case 2:
                                if ((Request != null) && (Request.QueryString[current.ParamValue] != null))
                                {
                                    str2 = str2 + " set " + current.ParamName + " = '" + Request.QueryString[current.ParamValue] + "' ";
                                }
                                break;

                            case 3:
                                if ((Request != null) && (Request.Form[current.ParamValue] != null))
                                {
                                    str2 = str2 + " set " + current.ParamName + " = '" + Request.Form[current.ParamValue] + "' ";
                                }
                                break;

                            case 4:
                                str2 = str2 + " set " + current.ParamName + " = getdate() ";
                                break;

                            case 5:
                                str2 = str2 + " set " + current.ParamName + " = '" + Conversions.ToString(PortalId) + "' ";
                                break;

                            case 6:
                                str2 = str2 + " set " + current.ParamName + " = '" + Conversions.ToString(UserId) + "' ";
                                break;

                            case 7:
                                str2 = str2 + " set " + current.ParamName + " = '" + Lang + "' ";
                                break;
                        }
                    }
                    strSQL = str2 + " " + strSQL;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return strSQL;
        }

        public void popDataGridSQL(string SQLcommand, DataGrid GridView)
        {
            DataProvider.Instance().popDataGridSQL(SQLcommand, GridView);
        }

        public string runXSL(NB_Store_SQLReportInfo objRInfo)
        {
            string str2 = "";
            ArrayList sQLReportXSLList = this.GetSQLReportXSLList(objRInfo.ReportID);
            if (sQLReportXSLList.Count > 0)
            {
                IEnumerator enumerator;
                try
                {
                    enumerator = sQLReportXSLList.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        NB_Store_SQLReportXSLInfo current = (NB_Store_SQLReportXSLInfo) enumerator.Current;
                        if (current.XMLInput == "")
                        {
                            str2 = SharedFunctions.XSLTransInMemory(this.ExecuteSQLReportXml(objRInfo.SQL), current.XSLFile);
                        }
                        else
                        {
                            str2 = SharedFunctions.XSLTransInMemory(current.XMLInput, current.XSLFile);
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
                if (str2 == "")
                {
                    str2 = "HIDE_XSL_OUTPUT";
                }
            }
            return str2;
        }

        public int UpdateObjSQLReport(NB_Store_SQLReportInfo objInfo)
        {
            return DataProvider.Instance().UpdateSQLReport(objInfo.ReportID, objInfo.PortalID, objInfo.ReportName, objInfo.SQL, objInfo.SchedulerFlag, objInfo.SchStartHour, objInfo.SchStartMins, objInfo.SchReRunMins, objInfo.LastRunTime, objInfo.AllowExport, objInfo.AllowDisplay, objInfo.DisplayInLine, objInfo.EmailResults, objInfo.EmailFrom, objInfo.EmailTo, objInfo.ShowSQL, objInfo.ConnectionString, objInfo.ReportRef, objInfo.AllowPaging, objInfo.ReportTitle, objInfo.FieldDelimeter, objInfo.FieldQualifier);
        }

        public void UpdateObjSQLReportParam(NB_Store_SQLReportParamInfo objInfo)
        {
            DataProvider.Instance().UpdateSQLReportParam(objInfo.ReportParamID, objInfo.ReportID, objInfo.ParamName, objInfo.ParamType, objInfo.ParamValue, objInfo.ParamSource);
        }

        public void UpdateObjSQLReportXSL(NB_Store_SQLReportXSLInfo objInfo)
        {
            DataProvider.Instance().UpdateSQLReportXSL(objInfo.ReportXSLID, objInfo.ReportID, objInfo.XMLInput, objInfo.XSLFile, objInfo.OutputFile, objInfo.DisplayResults, objInfo.SortOrder);
        }

        public void UpdateSQLReportXSL(int ReportXSLID, int ReportID, string XMLInput, string XSLFile, string OutputFile, bool DisplayResults, int SortOrder)
        {
            DataProvider.Instance().UpdateSQLReportXSL(ReportXSLID, ReportID, XMLInput, XSLFile, OutputFile, DisplayResults, SortOrder);
        }
    }
}

